'use strict';
	angular.module('ksr.services',['ngResource'])
	// 
		.factory('jsonSvc', ['$resource',function ($resource) {
		return $resource('/ksr/:entityType/:type/:eid/:action/:param1/:param2/:param3.json',//if parameter "action" is empty, then /.json => .json
		{
			entityType:'@entityType',
			type:'@type',
			eid:'@eid',
		    action: '@action',
		    param1:'@param1',
		    param2:'@param2',
		    param3:'@param3'
	   	},
	   	{
		   	getItem:{
		   		method : 'GET',
			    headers: {'Accept': 'application/json'},
			    params: {action:"get"}
		   	},
		   	getListItem:{
		   		method : 'GET',
			    headers: {'Accept': 'application/json'},
			    params: {action:"list"}
		   	},
		   	changePassword:{
			     method: 'POST',
			     headers: {'Accept': 'application/json'},
			     params: {type:'profile',action:'changePassword'}
		   	},
		   	saveItem:{
		   		method: 'POST',
		   		headers: {'Accept': 'application/json'},
		   		params: {action:'save'}
		   	},
		   	getList:{
		   		method : 'GET',
			    headers: {'Accept': 'application/json'},
			    params: {action:'list'},
			    isArray: true,
			    cache:false
		   	},
		   	getListBy:{ //send a json object
		   		method : 'POST',
			    headers: {'Accept': 'application/json'},
			    params: {action:'list'},
			    isArray: true
		   	},
		   	removeItem:{ //remove an item
		   		method : 'GET',
			    headers: {'Accept': 'application/json'},
			    params: {action:'remove'}
		   	},
		   	removeItemBy:{ //remove an item, POST
		   		method : 'POST',
			    headers: {'Accept': 'application/json'},
			    params: {action:'remove'}
		   	}

	   	});
	}])

	.factory('ksrCacheSvc',['$cacheFactory','$q','jsonSvc',
		function($cacheFactory,$q,jsonSvc){
			var cache = $cacheFactory('ksrCacheSvc'),
				getData=function(itemName,jsonObj){
					if (itemName==="categoryList") return jsonSvc.getList({entityType:'business',type:'category'});
					else if (itemName==="baseUrl") return jsonSvc.getItem({type:'base-url'});
					else if (itemName==="business") return jsonSvc.getItem({entityType:'business',type:'profile'});
					else if (itemName==="customer") return jsonSvc.getItem({entityType:'customer',type:'profile'});
					else if (itemName==="officeList") return jsonSvc.getList({entityType:'business',type:'office'});
					else if (itemName==="customerReviewList") {
						if (jsonObj&&jsonObj.id){
							if (jsonObj.rating&&jsonObj.offset&&jsonObj.pagesize)
								return jsonSvc.getListItem({entityType:'customer',type:'review',eid:jsonObj.id,param1:jsonObj.rating,param2:jsonObj.offset,param3:jsonObj.pagesize});
							else if (angular.isDefined(jsonObj.rating))
								return jsonSvc.getListItem({entityType:'customer',type:'review',eid:jsonObj.id,param1:jsonObj.rating});
							else
								return jsonSvc.getList({entityType:'customer',type:'review',eid:jsonObj.id});
						} 
					}
					else if (itemName==="officeReviewList") {
						if (jsonObj&&jsonObj.id){
							if (jsonObj.rating&&jsonObj.offset&&jsonObj.pagesize)
								return jsonSvc.getListItem({entityType:'business',type:'review',eid:jsonObj.id,param1:jsonObj.rating,param2:jsonObj.offset,param3:jsonObj.pagesize});
							else if (angular.isDefined(jsonObj.rating))
								return jsonSvc.getListItem({entityType:'business',type:'review',eid:jsonObj.id,param1:jsonObj.rating});
							else
								return jsonSvc.getList({entityType:'business',type:'review',eid:jsonObj.id});
						} 
					}
					else if (itemName==="eventList") return jsonSvc.getList({entityType:'business',type:'event'});
					else if (itemName==="eventScheduleList") return jsonSvc.getListBy({entityType:'business',type:'event-schedule'},jsonObj);
					else if (itemName==="eventSubscriptionList") return jsonSvc.getList({entityType:'customer',type:'event-subscription'});
				};
				
		return  {
			get:function(cacheName,fromCache,json){
				console.log("ksrCacheSvc.get="+cacheName);
				fromCache = (angular.isDefined(fromCache))?fromCache:"true";//default is use cache data
				var deferred = $q.defer();
				var retObj = cache.get(cacheName);
				if (retObj&&fromCache){
					deferred.resolve(retObj);
				} else {
					var obj = getData(cacheName,json);
					if (obj){
						obj.$promise.then(function(data){
							cache.put(cacheName,data);
							deferred.resolve(data);
						});
					}
				}
				return deferred.promise;
			}
			,getItemById:function(id,cacheName,fromCache,json){
				var list = cache.get(cacheName);
				var deferred = $q.defer();
				var id = parseInt(id);
				if (list&&fromCache){
					var o;
					angular.forEach(list, function(obj){
						if (id===obj.id){
							o=obj;
						}
					})
					deferred.resolve(o);
				} else {
					this.get(cacheName,fromCache,json).then(function(data){
						list = data;
						var o;
						angular.forEach(list, function(obj){
							if (id===obj.id){
								o=obj;
							}
						})
						deferred.resolve(o);
					})
				}
				return deferred.promise;
			}
			
		};
	}])

